(function(){
	var resizeStartX,resizeStartY,resizeStartW,resizeStartH,lastW,lastH;

	function onMouseDown(evt){
		resizeStartX=evt.data.x;
		resizeStartY=evt.data.y;
		resizeStartW=top.innerWidth;
		resizeStartH=top.innerHeight;
		lastW=resizeStartW;
		lastH=resizeStartH;
		window.listeners.send("win-resize-start-before",{x:resizeStartW,y:resizeStartH});
		evt.data.event.stop();
	}
	
	window.listeners.add("win-resize-start-before",function(){
		top.addEventListener("mousemove",onMouseMove,false);
		top.addEventListener("mouseup",onMouseUp,false);
		
	},Infinity);
	
	function onMouseMove(evt){
		var x=Math.max(resizeStartW+evt.screenX-resizeStartX,480);
		var y=Math.max(resizeStartH+evt.screenY-resizeStartY,320);
		
		lastW=x;
		lastH=y;
		top.resizeTo(x,y);
		evt.stop();
		window.listeners.send("resize-temporary-after",{x:x,y:y});
	}
	
	function onMouseUp(evt){
		top.removeEventListener("mousemove",onMouseMove,false);
		top.removeEventListener("mouseup",onMouseUp,false);
		
		evt.stop();
		window.listeners.send("resize-temporary-after",{x:x,y:y});
	}
	
	window.listeners.add("resize",onMouseDown);
})();

window.addEventListener("resize",function(evt){
	if(top.windowState!=3)
		document.body.setAttribute("fullscreen","true");
	else
		document.body.setAttribute("fullscreen","false");
},false);